package cn.edu.xjtu.android.emoji_warfare_android.game.hero;

import android.graphics.Canvas;
import cn.edu.xjtu.android.emoji_warfare_android.game.Bullet;
import cn.edu.xjtu.android.emoji_warfare_android.game.BulletsBuilder;
import cn.edu.xjtu.android.emoji_warfare_android.game.Config;
import cn.edu.xjtu.android.emoji_warfare_android.game.Engine;
import cn.edu.xjtu.android.emoji_warfare_android.game.Laser;
import cn.edu.xjtu.android.emoji_warfare_android.game.Sheild;

/**
 * Player<br>
 * 玩家
 * 
 * @author bill
 *
 */
public class Player extends Character {
    private boolean debug = false;

    Engine engine;
    public int exp = 0;
    private int weaponCount = 1;
    private int atk = 1;
    private int weaponAtk = 1;

    private int weapon;
    private long weaponOverTime;

    public boolean inDamage;

    public boolean canDamage;

    private int count;

    private Laser mLaser;

    private Sheild mSheild;

    /**
     * 构造一个Player
     * 
     * @param engine
     *            游戏引擎
     */
    public Player(Engine engine) {
        super(0, Config.Hero.playerOutline);
        hp = (int) (Math.sqrt(level + 5) * 5);
        this.basePoint.x = Config.screenWidth / 2.0f;
        this.basePoint.y = Config.screenHeight - 200.0f;
        this.engine = engine;
        weapon = 0;
        inDamage = false;
        count = 0;
        canDamage = true;
    }

    /**
     * 获取玩家当前攻击力
     * 
     * @return 攻击力
     */
    public int getATK() {
        return atk;
    }

    /**
     * 增加攻击力
     */
    public void addATK() {
        weaponCount++;
        resetATK();
    }

    public void resetATK() {
        if (weaponCount < 10) {
            atk = level + weaponCount / 5;
        } else {
            atk = level + (int) (Math.sqrt(weaponCount));
        }
        if (level <= 8) {
            weaponAtk = atk / (level + 1) + 2;
        } else {
            weaponAtk = atk / ((int) (Math.sqrt(level + 1))) + 2;
        }
    }

    @Override
    public boolean onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制受伤效果
        if (inDamage) {
            count++;
            if (count < 5) {
                canvas.drawCircle(0, 0, Config.Hero.radius - 13, Enemy.paint);
            }
            if (count == 9) {
                inDamage = false;
                count = 0;
            }
        }
        // debug用
        if (debug) {
            canvas.drawText(String.valueOf(atk), Config.Hero.radius - 20, 20 - Config.Hero.radius, Enemy.paint);
        }
        return false;
    }

    /**
     * 获取武器超时时间
     * 
     * @return 超时时间
     */
    public long getWeaponOverTime() {
        return weaponOverTime;
    }

    /**
     * 设置武器有效时间
     * 
     * @param weaponOverTime
     *            超时时间
     */
    public void setWeaponOverTime(long weaponOverTime) {
        this.weaponOverTime = weaponOverTime;
    }

    /**
     * 获取当前武器编号
     * 
     * @return 武器编号
     */
    public int getWeapon() {
        return weapon;
    }

    /**
     * 设置当前武器
     * 
     * @param weapon
     *            武器编号
     */
    public void setWeapon(int weapon) {
        this.weapon = weapon;
    }

    /**
     * 发射普通子弹
     */
    public void fire() {
        engine.addPlayerBullet(
                new Bullet(basePoint.x, basePoint.y - Config.Hero.radius, 0.0f, -Config.Bullet.speed, atk, 0));
    }

    /**
     * 发射S弹
     */
    public void fireScattershot() {
        engine.addPlayerBullets(BulletsBuilder.buildScattershot(basePoint.x, basePoint.y - Config.Hero.radius,
                weaponAtk, Config.Bullet.speed, 60, -90, level / 2 + 1, 0));
    }

    /**
     * 发射P弹
     */
    public void fireParallel() {
        engine.addPlayerBullets(BulletsBuilder.buildParallel(basePoint.x, basePoint.y - Config.Hero.radius, weaponAtk,
                Config.Bullet.speed, -90, Config.Hero.radius * 4.0f, level / 2, 0));
    }

    /**
     * 发射L弹
     */
    public void fireLine() {
        engine.addPlayerBullets(BulletsBuilder.buildLine(basePoint.x, basePoint.y - Config.Hero.radius, weaponAtk,
                Config.Bullet.speed, -90, Config.Hero.radius * 2.0f, level / 2, 0));
    }

    /**
     * 打开盾牌
     * 
     * @param delay
     *            盾牌开启时长
     */
    public void openSheild(long delay) {
        Sheild.stopTime = engine.clock() + delay;
        if (mSheild == null) {
            mSheild = new Sheild();
            mSheild.basePoint.x = basePoint.x;
            mSheild.basePoint.y = basePoint.y;
            engine.Layers.Player.add(mSheild);
            canDamage = false;
        }
    }

    /**
     * 关闭盾牌
     */
    public void stopSheild() {
        if (mSheild != null) {
            mSheild.basePoint.x = -1000;
            mSheild = null;
            canDamage = true;
        }
    }

    /**
     * 打开激光
     */
    public void openLaser() {
        if (mLaser == null) {
            mLaser = new Laser();
            mLaser.basePoint.x = basePoint.x;
            mLaser.basePoint.y = basePoint.y;
            engine.Layers.PlayerBullet.add(mLaser);
        }
    }

    /**
     * 关闭激光
     */
    public void stopLaser() {
        if (mLaser != null) {
            mLaser.basePoint.x = -1000;
            mLaser = null;
            Engine.instance.hasLaser = false;
        }
    }
}
